package org.microframework.cloud.auth.config;

import java.util.Properties;

import org.microframework.cloud.auth.config.properties.KaptchaProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/**
 * 验证码配置
 * 等保要求：验证码应具备防机器自动识别能力
 */
@Configuration
public class KaptchaConfig {
    
    @Autowired
    private KaptchaProperties properties;
    
    @Bean
    public DefaultKaptcha kaptcha() {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties props = new Properties();
        
        // 验证码字符配置
        props.setProperty("kaptcha.textproducer.char.string", properties.getTextproducer().getChar_().getString());
        props.setProperty("kaptcha.textproducer.char.length", String.valueOf(properties.getTextproducer().getChar_().getLength()));
        props.setProperty("kaptcha.textproducer.char.space", String.valueOf(properties.getTextproducer().getChar_().getSpace()));
        
        // 字体配置
        props.setProperty("kaptcha.textproducer.font.names", properties.getTextproducer().getFont().getNames());
        props.setProperty("kaptcha.textproducer.font.size", String.valueOf(properties.getTextproducer().getFont().getSize()));
        props.setProperty("kaptcha.textproducer.font.color", properties.getTextproducer().getFont().getColor());
        
        // 图片样式
        props.setProperty("kaptcha.image.width", String.valueOf(properties.getImage().getWidth()));
        props.setProperty("kaptcha.image.height", String.valueOf(properties.getImage().getHeight()));
        
        // 背景配置
        props.setProperty("kaptcha.background.clear.from", properties.getBackground().getClear().getFrom());
        props.setProperty("kaptcha.background.clear.to", properties.getBackground().getClear().getTo());
        
        // 边框配置
        props.setProperty("kaptcha.border", properties.getBorder());
        
        // 干扰线配置
        props.setProperty("kaptcha.noise.impl", properties.getNoise().getImpl());
        props.setProperty("kaptcha.noise.color", properties.getNoise().getColor());
        
        // 文字渲染器
        props.setProperty("kaptcha.word.impl", properties.getWord().getImpl());
        
        kaptcha.setConfig(new Config(props));
        return kaptcha;
    }
} 